package org.br.erp.project.project.service.impl;

import org.br.erp.project.project.entity.Project;
import org.br.erp.project.project.entity.Projectitem;
import org.br.erp.project.project.entity.Projectplannedtime;
import org.br.erp.project.project.entity.Projectsalesitem;
import org.br.erp.project.project.entity.Projectpoitem;
import org.br.erp.project.project.entity.Projectexpense;
import org.br.erp.project.project.mapper.ProjectitemMapper;
import org.br.erp.project.project.mapper.ProjectplannedtimeMapper;
import org.br.erp.project.project.mapper.ProjectsalesitemMapper;
import org.br.erp.project.project.mapper.ProjectpoitemMapper;
import org.br.erp.project.project.mapper.ProjectexpenseMapper;
import org.br.erp.project.project.mapper.ProjectMapper;
import org.br.erp.project.project.service.IProjectService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 项目
 * @Author: jeecg-boot
 * @Date:   2024-05-08
 * @Version: V1.0
 */
@Service
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService {

	@Autowired
	private ProjectMapper projectMapper;
	@Autowired
	private ProjectitemMapper projectitemMapper;
	@Autowired
	private ProjectplannedtimeMapper projectplannedtimeMapper;
	@Autowired
	private ProjectsalesitemMapper projectsalesitemMapper;
	@Autowired
	private ProjectpoitemMapper projectpoitemMapper;
	@Autowired
	private ProjectexpenseMapper projectexpenseMapper;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveMain(Project project, List<Projectitem> projectitemList,List<Projectplannedtime> projectplannedtimeList,List<Projectsalesitem> projectsalesitemList,List<Projectpoitem> projectpoitemList,List<Projectexpense> projectexpenseList) {
		projectMapper.insert(project);
		if(projectitemList!=null && projectitemList.size()>0) {
			for(Projectitem entity:projectitemList) {
				//外键设置
				entity.setParentid(project.getId());
				projectitemMapper.insert(entity);
			}
		}
		if(projectplannedtimeList!=null && projectplannedtimeList.size()>0) {
			for(Projectplannedtime entity:projectplannedtimeList) {
				//外键设置
				entity.setParentid(project.getId());
				projectplannedtimeMapper.insert(entity);
			}
		}
		if(projectsalesitemList!=null && projectsalesitemList.size()>0) {
			for(Projectsalesitem entity:projectsalesitemList) {
				//外键设置
				entity.setParentid(project.getId());
				projectsalesitemMapper.insert(entity);
			}
		}
		if(projectpoitemList!=null && projectpoitemList.size()>0) {
			for(Projectpoitem entity:projectpoitemList) {
				//外键设置
				entity.setParentid(project.getId());
				projectpoitemMapper.insert(entity);
			}
		}
		if(projectexpenseList!=null && projectexpenseList.size()>0) {
			for(Projectexpense entity:projectexpenseList) {
				//外键设置
				entity.setParentid(project.getId());
				projectexpenseMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateMain(Project project,List<Projectitem> projectitemList,List<Projectplannedtime> projectplannedtimeList,List<Projectsalesitem> projectsalesitemList,List<Projectpoitem> projectpoitemList,List<Projectexpense> projectexpenseList) {
		projectMapper.updateById(project);
		
		//1.先删除子表数据
		projectitemMapper.deleteByMainId(project.getId());
		projectplannedtimeMapper.deleteByMainId(project.getId());
		projectsalesitemMapper.deleteByMainId(project.getId());
		projectpoitemMapper.deleteByMainId(project.getId());
		projectexpenseMapper.deleteByMainId(project.getId());
		
		//2.子表数据重新插入
		if(projectitemList!=null && projectitemList.size()>0) {
			for(Projectitem entity:projectitemList) {
				//外键设置
				entity.setParentid(project.getId());
				projectitemMapper.insert(entity);
			}
		}
		if(projectplannedtimeList!=null && projectplannedtimeList.size()>0) {
			for(Projectplannedtime entity:projectplannedtimeList) {
				//外键设置
				entity.setParentid(project.getId());
				projectplannedtimeMapper.insert(entity);
			}
		}
		if(projectsalesitemList!=null && projectsalesitemList.size()>0) {
			for(Projectsalesitem entity:projectsalesitemList) {
				//外键设置
				entity.setParentid(project.getId());
				projectsalesitemMapper.insert(entity);
			}
		}
		if(projectpoitemList!=null && projectpoitemList.size()>0) {
			for(Projectpoitem entity:projectpoitemList) {
				//外键设置
				entity.setParentid(project.getId());
				projectpoitemMapper.insert(entity);
			}
		}
		if(projectexpenseList!=null && projectexpenseList.size()>0) {
			for(Projectexpense entity:projectexpenseList) {
				//外键设置
				entity.setParentid(project.getId());
				projectexpenseMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		projectitemMapper.deleteByMainId(id);
		projectplannedtimeMapper.deleteByMainId(id);
		projectsalesitemMapper.deleteByMainId(id);
		projectpoitemMapper.deleteByMainId(id);
		projectexpenseMapper.deleteByMainId(id);
		projectMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			projectitemMapper.deleteByMainId(id.toString());
			projectplannedtimeMapper.deleteByMainId(id.toString());
			projectsalesitemMapper.deleteByMainId(id.toString());
			projectpoitemMapper.deleteByMainId(id.toString());
			projectexpenseMapper.deleteByMainId(id.toString());
			projectMapper.deleteById(id);
		}
	}

	@Override
	public void updateProjectStatus(List<String> ids, String status) {
		List<Project> projects = projectMapper.selectBatchIds(ids);
		for (Project project : projects) {
			project.setProjectstatus(status);
			projectMapper.updateById(project);
		}
	}

}
